package com.linwen.excise.search;
/**
 * 前提条件为元素已经有序
 * */
public class binarySearch {
    public static void main(String[] args) {
        int[] arr = new int[]{0,2,3,4,6,8,10,12};
        int target = 12;
        System.out.println(binarySearch(arr,target));
    }
    private static int binarySearch(int[] arr, int target) {
        int start = 0;
        int end = arr.length-1;
        int mid = (start+end)/2;
        while (start<=end){
            if(target==arr[mid]){
                return mid;
            }
            if(target<arr[mid]){
                end = mid-1;
                mid = (start+end)/2;
            }
            if(target>arr[mid]){
                start = mid+1;
                mid = (start+end)/2;
            }
        }
        return -1;
    }


}
